package com.health.widget;

import com.health.R;
import com.health.tools.GlobalConts;
import com.health.tools.ImageTools;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * 弹出框
 * Created by nova on 2017/11/29.
 */

public class CommomDialog extends Dialog implements View.OnClickListener{
    private TextView contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;
    
    private ImageView img_content;
    private ImageView  img_content2;
    

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;
    private int imgResId=-1;//图片资源
    private String imgRes2Base64String="";//图片资源2  （Base64String）
    private int negativeVisId=-1;//左边按钮是否可见

    public CommomDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public CommomDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }

    public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }

    protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public CommomDialog setImageContent(int imgResId){
    	this.imgResId= imgResId;
        return this;
    }
    
    public CommomDialog setImage2Content(String base64String){
    	this.imgRes2Base64String= base64String;
        return this;
    }
    
    public CommomDialog setTitle(String title){
        this.title = title;
        return this;
    }

    public CommomDialog setPositiveButton(String name){
        this.positiveName = name;
        return this;
    }

    public CommomDialog setNegativeButton(String name){
        this.negativeName = name;
        
        return this;
    }
    public CommomDialog setNegativeButtonVisibility(int vis){
        this.negativeVisId = vis;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.normal_dialog);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView(){
        contentTxt = (TextView)findViewById(R.id.content);
        titleTxt = (TextView)findViewById(R.id.title);
        submitTxt = (TextView)findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView)findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);
        img_content=(ImageView)findViewById(R.id.img_content);
        img_content2=(ImageView)findViewById(R.id.img_content2);
        
        
        contentTxt.setText(content);
        if(!TextUtils.isEmpty(positiveName)){
            submitTxt.setText(positiveName);
        }

        if(!TextUtils.isEmpty(negativeName)){
            cancelTxt.setText(negativeName);
        }

        if(!TextUtils.isEmpty(title)){
            titleTxt.setText(title);
        }
        
        if(imgResId!=-1){
        	 img_content.setVisibility(View.VISIBLE);
             img_content.setImageResource(imgResId);
        }
        
        if(!TextUtils.isEmpty(imgRes2Base64String)){
        	 img_content2.setVisibility(View.VISIBLE);
             img_content2.setImageBitmap(ImageTools.base64ToBitmap(imgRes2Base64String)); 
        }
        if(negativeVisId!=-1){
        	cancelTxt.setVisibility(negativeVisId);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cancel:
                if(listener != null){
                    listener.onClick(this, false);
                }
                this.dismiss();
                break;
            case R.id.submit:
                if(listener != null){
                    listener.onClick(this, true);
                }
                break;
        }
    }

    public interface OnCloseListener{
        void onClick(Dialog dialog, boolean confirm);
    }
}
